﻿using UnityEngine;
using System.Collections;

public class ActiveCardFrameAlpha : MonoBehaviour {
	
	UISprite sprite;
	public bool bGlow;
	public bool bStop;
	float alpha;
	float alpha_delta;
	Color reserveColor;
	
	void Awake () {
		sprite = GetComponent<UISprite>();
		reserveColor = sprite.color;
	}
	
	public void StartGlow()
	{
		if(bGlow)
			return;
		gameObject.SetActive(true);
		bGlow = true;
		alpha =  Random.Range(0.0f, 0.4f);
		bStop=false;
		alpha_delta=0.8f;
	}
	
	public void StopGlow()
	{
		bStop = true;
		alpha_delta = -1;
	}
	
	// Update is called once per frame
	void Update () {
		
		if(bGlow)
		{
			alpha += alpha_delta * Time.deltaTime;
			if(alpha <0.4f) {
				if(!bStop)
				{
					alpha = 0.4f;
					alpha_delta = -alpha_delta;
				}
			}
			if(alpha<=0)
			{
				alpha=0;
				bGlow=false;
				bStop=false;
				gameObject.SetActive(false);
			}
			if(alpha >1.0f) {
				alpha = 1.0f;
				alpha_delta = -alpha_delta;
			}
			sprite.color = new Color(reserveColor.r, reserveColor.g, reserveColor.b, alpha);
		}	
	}
}
